package org.easy.tencim.util;



import java.io.IOException;
import java.util.Base64;

/**
 * @author: Li chaojie
 * @Date: 2022/3/23
 * @Description:
 */
public class Base64URL {
    public static byte[] base64EncodeUrl(byte[] input) {
        Base64.Encoder encoder = Base64.getEncoder();
        byte[] base64 = encoder.encode(input);

        for (int i = 0; i < base64.length; ++i) {
            switch (base64[i]) {
                case 43:
                    base64[i] = 42;
                    break;
                case 47:
                    base64[i] = 45;
                    break;
                case 61:
                    base64[i] = 95;
            }
        }

        return base64;
    }

    public static byte[] base64DecodeUrl(byte[] input) throws IOException {
        byte[] base64 = (byte[]) input.clone();

        for (int i = 0; i < base64.length; ++i) {
            switch (base64[i]) {
                case 42:
                    base64[i] = 43;
                    break;
                case 45:
                    base64[i] = 47;
                    break;
                case 95:
                    base64[i] = 61;
            }
        }
        Base64.Decoder decoder = Base64.getDecoder();
        return decoder.decode(base64.toString());
    }
}
